using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class MultiplayerConnectMain : MonoBehaviour
{
	[Serializable]
	public class PlayerDescription
	{
		[Serializable]
		public class ConnectionTry
		{
			public int numConnectionTries = 10;

			public float timeBetweenConnectTries = 0.1f;

			public float maxSkillLevelDifference = 1000f;

			public FloatRange waitInRoom = new FloatRange
			{
				min = 8f,
				max = 13f
			};

			public int repeatTimes = 1;
		}

		public string name;

		public FloatRange timeToWaitTillActive;

		public float skillLevel;

		[Header("Connection Params")]
		public int numConnectTries = 10;

		public float timeBetweenConnectTries = 0.1f;

		public float maxSkillLevelDifference = 1000f;

		public FloatRange waitInRoom = new FloatRange
		{
			min = 8f,
			max = 13f
		};

		public List<ConnectionTry> connectionTry = new List<ConnectionTry>();

		public PlayerDescription Clone()
		{
			PlayerDescription playerDescription = new PlayerDescription();
			playerDescription.name = name;
			playerDescription.timeToWaitTillActive = timeToWaitTillActive;
			playerDescription.skillLevel = skillLevel;
			playerDescription.numConnectTries = numConnectTries;
			playerDescription.timeBetweenConnectTries = timeBetweenConnectTries;
			playerDescription.maxSkillLevelDifference = maxSkillLevelDifference;
			playerDescription.waitInRoom = waitInRoom;
			playerDescription.connectionTry = new List<ConnectionTry>();
			playerDescription.connectionTry.AddRange(connectionTry);
			return playerDescription;
		}
	}

	public GameObject matchTestPlayerPrefab;

	public PhotonClone photonClone;

	public bool chooseRandomPlayers;

	public bool eachPlayerSeparate;

	public bool prewarm;

	public int numRandomPlayers;

	public List<PlayerDescription> players = new List<PlayerDescription>();

	protected List<MatchTestPlayer> activeTesters = new List<MatchTestPlayer>();

	private void Awake()
	{
		if (chooseRandomPlayers)
		{
			if (eachPlayerSeparate)
			{
				StartCoroutine(RunTestRandomSeparate());
			}
			else
			{
				StartCoroutine(RunTestRandom());
			}
		}
		else
		{
			StartCoroutine(RunTest());
		}
	}

	private IEnumerator RunTestRandom()
	{
		for (int playerIndex = 0; playerIndex < numRandomPlayers; playerIndex++)
		{
			PlayerDescription player = players[UnityEngine.Random.Range(0, players.Count)].Clone();
			if (string.IsNullOrEmpty(player.name))
			{
				player.name = "P";
			}
			player.name = player.name + "-" + playerIndex;
			float timeToWait = player.timeToWaitTillActive.Random();
			if (timeToWait > 0f)
			{
				yield return new WaitForSeconds(timeToWait);
			}
			CreateTester(player);
		}
	}

	private IEnumerator RunTestRandomSeparate()
	{
		int playerIndex = 0;
		List<float> times = new List<float>();
		while (playerIndex < numRandomPlayers)
		{
			for (int i = 0; i < players.Count; i++)
			{
				PlayerDescription playerDescription = players[i];
				if (i >= times.Count)
				{
					if (prewarm)
					{
						times.Add(0f);
					}
					else
					{
						times.Add(playerDescription.timeToWaitTillActive.Random());
					}
				}
				times[i] -= Time.deltaTime;
				if (times[i] <= 0f)
				{
					times[i] = Mathf.Abs(times[i]) + playerDescription.timeToWaitTillActive.Random();
					playerDescription = playerDescription.Clone();
					if (string.IsNullOrEmpty(playerDescription.name))
					{
						playerDescription.name = "P";
					}
					PlayerDescription playerDescription2 = playerDescription;
					playerDescription2.name = playerDescription2.name + "-" + playerIndex;
					CreateTester(playerDescription);
					playerIndex++;
				}
			}
			yield return null;
		}
	}

	private IEnumerator RunTest()
	{
		int playerIndex = 0;
		foreach (PlayerDescription player in players)
		{
			if (string.IsNullOrEmpty(player.name))
			{
				player.name = "P-" + playerIndex;
			}
			float timeToWait = player.timeToWaitTillActive.Random();
			if (timeToWait > 0f)
			{
				yield return new WaitForSeconds(timeToWait);
			}
			CreateTester(player);
			playerIndex++;
		}
	}

	private void CreateTester(PlayerDescription player)
	{
		GameObject gameObject = UnityEngine.Object.Instantiate(matchTestPlayerPrefab);
		gameObject.gameObject.name = player.name;
		MatchTestPlayer component = gameObject.GetComponent<MatchTestPlayer>();
		component.Init(player, photonClone);
	}
}
